import java.applet.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.*;
import java.util.*;
import javax.swing.*;

public class HuntPanel extends JPanel {
	private ArrayList<Animal> animalList = new ArrayList<Animal>();
	private Random rand;
	private final int MAX_X = 500, MAX_Y = 350;
	private Player player;
	private JButton returnButton;
	private JLabel bulletLabel;
	private boolean stillHunting;
	private AudioClip song; // Sound player
	private URL songPath;

	/**
	 * Create the panel.
	 */
	public HuntPanel(Player p) {
		setLayout(null);
		setBackground(new Color(0,100,0));
		this.addMouseListener(new MouseControl());
		this.rand = new Random();
		this.player = p;
		this.stillHunting = true;

		returnButton = new JButton("Return to Trail");
		returnButton.setBounds(383, 271, 117, 29);
		returnButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				stillHunting = false;				
			}
			
		});
		add(returnButton);
		
		bulletLabel = new JLabel("Bullets: "+player.getBullets());
		bulletLabel.setBounds(305, 277, 112, 16);
		add(bulletLabel);
	
		//animalList.add(new Bunny(0,0,"right"));
		//animalList.add(new Bunny(MAX_X-30,120,"left"));
	}
	
	/**
	 * Runs the game
	 */
	public void run() {
		JOptionPane.showMessageDialog(null, "You have reached a nearby field to hunt. You will stay\nhere until " +
				"you run out of ammunition or click Return to Trail.");
		int i = 0;
		while (player.getBullets() > 0 && stillHunting) {
			checkForAnimal();
			repaint();
			update();
			
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		endHunting();
	}
	

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		for (int i = 0; i < animalList.size(); i++) {
			Animal temp = animalList.get(i);
			temp.paint((Graphics2D) g);
		}
	}

	/**
	 * Moves all of the animals
	 */
	public void update() {
		for (int i = 0; i < animalList.size(); i++) {
			animalList.get(i).move();
		}
	}
	
	private void endHunting() {
		if (stillHunting == false) {
			int meat = player.calculateMeat();
			if (meat > 250) {
				JOptionPane.showMessageDialog(null, "Very successful hunting trip! You collected "+meat+" pounds of" +
						"meat, but you can only carry 250 back to camp.");
				// update party data
			}
			else {
				JOptionPane.showMessageDialog(null, "You have collected "+meat+" pounds of meat to bring back to camp");
				// update party data
			}
			
			
		}
		
		else if (player.getBullets() == 0) {
			int meat = player.calculateMeat();
			if (meat > 250) {
				JOptionPane.showMessageDialog(null, "You have run out of ammo! Even though your hunting trip has ended early," +
						"you collected "+meat+" pounds of meat! However, you can only carry 250 pounds back to camp");
				// update party data
			}
			else {
				JOptionPane.showMessageDialog(null, "You have run out of ammo! You still managed to collect "+meat+
						" pounds of meat to take back to camp.");
				// update party data
			}
			
		}
		
		//presenter.switchToMain();
	}
	
	private void checkForAnimal() {
		double gen = rand.nextGaussian(); // gen is a number from -inf to +inf
		gen = Math.abs(gen); // gen is now a number from 0 to inf
		
		int x = 0;
		int y = rand.nextInt(MAX_Y);
		int rand1 = rand.nextInt(100);
		String str = null;
		if (rand1 < 50) {
			str = "left";
		  	x = MAX_X-40;
		}
		else if (rand1 >= 50) {
			str = "right";
			x = 0;
		}
		
		
		if (gen < 2) { 
			
		}
		if (gen >= 2 && gen < 2.3) {
			animalList.add(new Bunny(x,y,str));
		}
		if(gen >= 2.3 && gen < 2.5) { 
			animalList.add(new Deer(x,y,str));
		}
		if (gen >= 2.5) { 
			//animalList.add(new Bear(x,y,str));
		} 
	
	}
	


	/**
	 * This is called when the mouse is clicked
	 */
	private class MouseControl extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			// update bullets
			player.shoot();
			bulletLabel.setText("Bullets: "+Integer.toString(player.getBullets()));
			
			// TESTING
			printTest(e.getX(), e.getY());
		
			// for each animal
			for (int i = 0; i < animalList.size(); i++) {
				Animal temp = animalList.get(i);

				// is the animal hit?
				if (temp.containsPoint(e.getX(), e.getY())) {
					player.kill(temp, temp.isDead());
					temp.kill();
					break;
				}
			}
			
			System.out.println(player.toString());
		}
		
		private void printTest(int x, int y) {
			System.out.println("Mouse: ("+x+", "+y+")");
			for( int i=0; i<animalList.size(); i++) {
				System.out.println("Animal "+i+": ( x:["+animalList.get(i).getXPos()+", "+animalList.get(i).getX2()+"], y:["+animalList.get(i).getYPos()+", "+animalList.get(i).getY2()+"] )");
			}
			System.out.println();
			
		}
	}
}
